﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Facebook;

using Sketchial.Data;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string uid = ((HiddenField)this.FindControl("hdnFB")).Value.ToString();
        try
        {
            HttpCookie cookie = Request.Cookies["TokenCookie"]; /// it returns a null even if the cookie exisits

            if (cookie == null)
            {
                Response.Redirect("~/pages/login.aspx", false);
            }
            else
            {
                string TOKEN = cookie.Values["TOKEN"].ToString();
                var accessToken = TOKEN;
                if (Context.Session["AccessToken"] != null)
                    accessToken = Context.Session["AccessToken"].ToString();
                var client = new FacebookClient(accessToken);
                dynamic result = client.Get("me", new { fields = "name,id" });
                string name = result.name;
                string id = result.id;
            }
        }
        catch (Exception ex)
        {
            string result = ex.Message;
        }
        //rpPlace.DataSource = GetFiles("place");
        //rpPlace.DataBind();

        //rpEvent.DataSource = GetFiles("event");
        //rpEvent.DataBind();

        //rpCountry.DataSource = GetFiles("countries");
        //rpCountry.DataBind();

        //rpActivity.DataSource = GetFiles("activity");
        //rpActivity.DataBind();

        //rpPeople.DataSource = GetFiles("people");
        //rpPeople.DataBind();


        //Objects Fill Table
        //FillObjectTable("place");
        //FillObjectTable("event");
        //FillObjectTable("countries");
        //FillObjectTable("activity");
        //FillObjectTable("people");
    }


    public void rp1_dataBound(object sender, EventArgs e)
    {

    }

    public DataTable GetFiles(string folder)
    {
        DataTable dt2 = new DataTable();
        dt2.Columns.Add("id");
        FileInfo fi = new FileInfo(Server.MapPath("~/Objects/" + folder + "/"));
        DirectoryInfo di = fi.Directory;
        FileInfo[] fsi = di.GetFiles();
        //Response.Write("The directory contains the following files and directories:" + di.FullName + "<hr>");
        foreach (FileSystemInfo info in fsi)
        {
            DataRow dr2 = dt2.NewRow();
            dr2[0] = info.Name;
            dt2.Rows.Add(dr2);
        }

        return dt2;
    }

    private void FillObjectTable(string className)
    {
        FileInfo fi = new FileInfo(Server.MapPath("~/Objects/" + className + "/"));
        DirectoryInfo di = fi.Directory;
        FileInfo[] fsi = di.GetFiles();
        //Response.Write("The directory contains the following files and directories:" + di.FullName + "<hr>");
        foreach (FileSystemInfo info in fsi)
        {
            TableHelper helper = new TableHelper();
            ObjectEntity ent = new ObjectEntity();
            ent.PartitionKey = "Object";
            ent.RowKey = info.Name;
            ent.ObjectID = info.Name;
            ent.ObjectName = info.FullName;
            ent.ObjectClass = className;
            helper.AddNewObject(ent);
        }
    }
}